﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using ECCIT;
using ECCIT.Utilities;

using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.Globalization;
using OgilvyOne.UKNow.Utility;
using OgilvyOne.UKNow.Model;

public partial class GateService_EventGate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.ContentType = "text/xml";
        Response.ContentEncoding = Encoding.UTF8;

        string output = string.Empty;
        try
        {
            ResultInfo valRes = ValidateArguments();

            if (valRes.Result)
            {
                string sLangID = Request.Params["lang"];
                string op = Request.Params["op"];
                
                int langID = (string.IsNullOrEmpty(sLangID)) ? 2052 : int.Parse(sLangID);
                EventsBLL eventsBll = new EventsBLL(langID);

                switch (op)
                {
                    case "parseShowDate":
                        string sd = Request.Params["sd"];
                        string ed = Request.Params["ed"];
                        string interval = Request.Params["interval"];
                        string ret = eventsBll.FormatEventsDate(sd, ed, interval);
                        ResultInfo res = new ResultInfo(true);
                        res.ReturnObject = ret;
                        output = JsonUtil.Serialize(res);
                        break;
                    case "getCityModels":
                        output = JsonUtil.Serialize(DictionaryUtil.GetCityModels());
                        break;
                    case "GetAllPublishedEventsYearMonth":
                        string cityID = Request.Params["city"];
                        string artFormID = Request.Params["artForm"];
                        output = JsonUtil.Serialize(eventsBll.GetAllPublishedEventsYearMonth(int.Parse(cityID), int.Parse(artFormID)));
                        break;
                    case "GetAllPublishedEventsArtForms":
                        ArtFormBLL afb = new ArtFormBLL(langID);
                        //output = JsonUtil.Serialize(eventsBll.GetAllPublishedEventsArtForms(int.Parse(Request.Params["city"])));
                        output = JsonUtil.Serialize(afb.GetArtFormModels());
                        break;
                    case "GetAllPublishedEventsDate":
                        cityID = Request.Params["city"];
                        artFormID = Request.Params["artForm"];
                        string yearMonth = Request.Params["yearmonth"];
                        output = JsonUtil.Serialize(eventsBll.GetAllPublishedEventsDate(int.Parse(cityID), int.Parse(artFormID), yearMonth));
                        break;
                    case "GetFilteredEventsModels":
                        int iCityID = int.Parse(Request.Params["city"]);
                        int iArtFormID = int.Parse(Request.Params["artForm"]);
                        yearMonth = Request.Params["yearmonth"];
                        int date = int.Parse(Request.Params["date"]);
                        int currPageIndex = int.Parse(Request.Params["currPageIndex"]);
                        output = JsonUtil.Serialize(eventsBll.GetFilteredEventsModels(iCityID, iArtFormID, yearMonth, date, currPageIndex));
                        break;
                    case "GetEventsByID":
                        int eid = int.Parse(Request.Params["eid"]);
                        output = JsonUtil.Serialize(eventsBll.selEvent(eid));
                        break;
                    case "GetEventsImagesByEventsID":
                        eid = int.Parse(Request.Params["eid"]);
                        EventsImageBLL eiBll = new EventsImageBLL(langID);
                        output = JsonUtil.Serialize(eiBll.GetPublishedEventsImageModels(eid));
                        break;
                    case "GetEventsCommentByID":
                        eid = int.Parse(Request.Params["eid"]);
                        int cid = int.Parse(Request.Params["cid"]);
                        EventsCommentBLL ecBll = new EventsCommentBLL(langID);
                        output = JsonUtil.Serialize(ecBll.selEvent(cid));
                        break;
                    case "GetRoundEventsHtmlFileName":
                        eid = int.Parse(Request.Params["eid"]);
                        EventsModel model = eventsBll.selEvent(eid);
                        string fileName = "";
                        if (model != null && !string.IsNullOrEmpty(model.Education))
                        {
                            fileName = eventsBll.GetEventsRoundFileName(model);
                        }
                        output = JsonUtil.Serialize(fileName);
                        break;
                    case "NewGuid":
                        output = JsonUtil.Serialize(Guid.NewGuid().ToString());
                        break;
                }
            }
        }
        catch (Exception ex)
        {
            output = "<files value=\"false\"></files>";
        }

        Response.Write(output);
    }

    private ResultInfo ValidateArguments()
    {
        ResultInfo result = new ResultInfo(false);
        string sLangID = Request.Params["lang"];
        if (string.IsNullOrEmpty(sLangID)) return result;

        int langID;
        bool b = int.TryParse(sLangID, out langID);
        if (!b) return result;
        if (langID != 2052 && langID != 1033) return result;

        result.Result = true;
        return result;
    }
}